#-*-coding:utf-8-*-
import web
import datetime
urls = (
	"/","index",
	"/add/$","add",
	"/reply/$","reply",
	"/delete/$","delete"
	)
app = web.application(urls,globals())
db      = web.database(dbn="mysql",db="hello",user="root",pw="2020361155") 
render  = web.template.render("templates/",cache=False)
class index:
	def GET(self):
		result = db.query("select id,title,reply,content,date from guestbook order by id DESC")		
		return render.index(result)


class add:
	def POST(self):
		info = web.input(title="",content="")
		if not info.title or not info.content:
			raise web.seeother("/")
		ip = web.ctx.env["REMOTE_ADDR"]
		d  = datetime.datetime.now()
		reply = 'no reply'
		sql = "insert into guestbook (title,content,reply,ip,date) values('%s','%s','%s','%s','%s')" %(info.title,info.content,reply,ip,d)
		db.query(sql)
		raise web.seeother("/")
class delete:
	def GET(self):
		info = web.input(id="")
		sql  = "delete from guestbook where id=%s" %info.id
		db.query(sql)
		raise web.seeother("/")
class reply:
	def POST(self):
		info = web.input(content='no reply',id='')
		sql  = "update guestbook set reply='%s' where id=%s" %(info.content,info.id)
		db.query(sql)
		raise web.seeother("/")
	def GET(self):
		info  = web.input(id="")
		sql   = "select id,title,content from guestbook where id = %s" % info.id
		result=db.query(sql)
		return render.reply(result[0])
if __name__=="__main__":
	app.run()

